package com.wei.czz.framework.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.common.vo.store.assign.AssignVo;
import com.wei.czz.framework.store.dao.AssignStoreFlowDao;
import com.wei.czz.framework.store.entity.AssignStoreFlowEntity;
import com.wei.czz.framework.store.service.AssignStoreFlowService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2023-12-01 11:38:11
 * className: AssignStoreFlowServiceImpl
 * version: 1.0
 * description:
 */
@Service("assignStoreFlowService")
public class AssignStoreFlowServiceImpl extends ServiceImpl<AssignStoreFlowDao, AssignStoreFlowEntity>
        implements AssignStoreFlowService {

    private static final Logger log = LoggerFactory.getLogger(AssignStoreFlowServiceImpl.class);

    @Transactional
    @Override
    public void saveAssignStoreFlow(AssignVo assignVo) {
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        List<AssignStoreFlowEntity> assignStoreFlowList = assignVo.getStoreVoList()
                .stream()
                .map(assignParamVo -> {
                    AssignStoreFlowEntity assignStoreFlow = new AssignStoreFlowEntity();
                    assignStoreFlow.setUserId(assignVo.getUserId())
                            .setMasterId(assignParamVo.getId())
                            .setStoreId(assignParamVo.getStoreId())
                            .setSubmitType(assignParamVo.getSubmitType())
                            .setAssignType(assignVo.getAssignType())
                            .setDeleteStatus(CommonEnum.ZERO.getValue())
                            .setCreateTime(date)
                            .setCreateUser(userPo.getUsername())
                            .setCreateUserId(userPo.getUserId());
                    return assignStoreFlow;
                })
                .collect(Collectors.toList());
        // 批量插入
        boolean bool = this.saveBatch(assignStoreFlowList);
        log.info("保存商店分单操作流水完成。bool={}", bool);
    }

    @Override
    public List<AssignStoreFlowEntity> getAssignStoreFlowList(Long storeId, Long masterId) {
        if (ObjectUtils.allNull(storeId, masterId)) {
            log.info("传入的商店主键和业务主键为空");
            return Collections.emptyList();
        }

        LambdaQueryWrapper<AssignStoreFlowEntity> assignStoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
        assignStoreLambdaQueryWrapper.eq(Objects.nonNull(masterId), AssignStoreFlowEntity::getMasterId, masterId)
                .eq(Objects.nonNull(storeId), AssignStoreFlowEntity::getStoreId, storeId)
                .eq(AssignStoreFlowEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<AssignStoreFlowEntity> assignStoreFlowList = baseMapper.selectList(assignStoreLambdaQueryWrapper);
        if (assignStoreFlowList.isEmpty()) {
            log.info("查询商店分单记录结果为空。 masterId={} storeId={}", masterId, storeId);
        }
        return assignStoreFlowList;
    }
}